var Assistant = {
		global:{},//全局变量
		tools:{},//工具类函数
		User:{},//用户数据
};

Assistant.global.rowNum = 30;//Grid的每页的记录数，默认值
Assistant.global.rowList = [ 30, 60, 100 ];//Grid的每页大小的下拉选择框

(function(){
/**
 * 验证器
 * 
 * @param validateTipsId，显示验证提示信息的DOM元素的ID
 */
function Validator(validateTipsId){
	this.validateTipsId = validateTipsId;
}

/**
 * 更新提示信息，提示信息显示在validateTipsId中<br>
 * 比如：
 * <p id="validateTips">
 * All form fields are required.
 * </p>
 * <br>
 * 该函数依赖于jQuery及jQuery UI
 */
Validator.prototype.updateTips = function(t) {
	$("#" + this.validateTipsId).text(t).effect("highlight", {}, 1500);
}

/**
 * 校验金额是否合法
 */
Validator.prototype.checkMoney = function(oMoney,msg) {
	if(!Assistant.tools.checkMoney(oMoney)){
		this.updateTips(msg);
		return false;
	}else{
		this.updateTips("");
		return true;
	}
}

/**
 * 校验指定(jQuery)对象的值的长度是否在min和max之间
 */
Validator.prototype.checkLength = function(o, name, min, max) {
	if (o.val().length > max || o.val().length < min) {
		o.addClass('ui-state-error');
		if(min == max){
			this.updateTips("' " + name + " '的长度必须为 " + min + ".");
		}else{
			this.updateTips("' " + name + " '的长度必须在 " + min + " 和 " + max + "之间.");
		}
		return false;
	} else {
		return true;
	}
}

/**
 * 校验object的值，如果等于value，则返回false，并显示msg.
 */
Validator.prototype.checkValue = function(object,value,msg){
	if(object.val() == value){
		this.updateTips(msg);
		return false;
	}else{
		return true;
	}
}

/**
 * 校验指定(jQuery)对象的值是否满足给出的正则表达式
 */
Validator.prototype.checkRegexp = function(o, regexp, n) {
	if (!(regexp.test(o.val()))) {
		o.addClass('ui-state-error');
		this.updateTips(n);
		return false;
	} else {
		return true;
	}
}

/**
 * 校验o的值是否等于0，如果是，返回false，并显示msg.
 */
Validator.prototype.checkZero = function(o,msg){
	var regexp = /^-?0{0,}$/;
	
	var value;
	if(o instanceof Object){
		value = o.val();
	}else{
		value = document.getElementById(o).value;
	}
	
	if (regexp.test(value)) {
		this.updateTips(msg);
		return false;
	} else {
		return true;
	}
}

Assistant.validator = Validator;
})();

/**
 * 格式化金额字符串，在金额前面加“￥”
 */
Assistant.tools.formatCurrency = function(num) {
    if(!num) return;
    num = num.toString().replace(/\$|\,/g,'');
    if(isNaN(num))
        num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num*100+0.50000000001);
    cents = num%100;
    num = Math.floor(num/100).toString();
    if(cents<10)
        cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
        num = num.substring(0,num.length-(4*i+3))+','+
              num.substring(num.length-(4*i+3));
    return (((sign)?'':'-') + '￥' + num + '.' + cents);
}

/**
 * 反格式化金额字符串，去掉金额字符串前面的“￥”
 */
Assistant.tools.unFormatCurrency = function(value) {
	var newValue = value.replace("￥","");
	newValue = newValue.replace(",","");
	return newValue;
}

/**
 * 验证给定的字符串是否是数字
 * 
 * @param str
 * @return 如果是数字，返回true；否则返回false.
 */
Assistant.tools.isDigit = function(str) {
	var patrn = /^-?\d+\.{0,}\d{0,}$/;

	if (!patrn.exec(str)) {
		return false
	} else {
		return true
	}
}

/**
 * 校验金额是否合法
 */
Assistant.tools.checkMoney = function(oMoney) {
	var money;
	if(oMoney instanceof Object){
		money = oMoney.val();
	}else{
		money = document.getElementById(oMoney).value;
	}
	var isDigit = Assistant.tools.isDigit(money);
	if (isDigit == false) {
		return false;
	} else {
		return true;
	}
}

/**
 * 校验指定(jQuery)对象的值是否满足给出的正则表达式
 */
Assistant.tools.checkRegexp = function(o, regexp) {
	if (!(regexp.test(o.val()))) {
		return false;
	} else {
		return true;
	}
}

/**
 * 校验o的值是否等于0，如果是，返回false，并显示msg.
 */
Assistant.tools.checkZero = function(o){
	var regexp = /^-?0{0,}$/;
	
	var value;
	if(o instanceof Object){
		value = o.val();
	}else{
		value = document.getElementById(o).value;
	}
	
	if (regexp.test(value)) {
		return false;
	} else {
		return true;
	}
}

/**
 * 获取下拉框中选中的元素的值
 */
Assistant.tools.getSelectedValue = function(selectId) {
	var select = document.getElementById(selectId);
	return select.options[select.selectedIndex].value;
}

/**
 * 获取下拉框中选中的元素的标签
 */
Assistant.tools.getSelectedText = function(selectId) {
	var select = document.getElementById(selectId);
	return select.options[select.selectedIndex].text;
}

/**
 * 获取当前日期（yyyy-MM-dd）
 */
Assistant.tools.getNowDate = function() {
	var now=new Date();
	var year = now.getFullYear();
	var month = now.getMonth() + 1;
	var day = now.getDate();
	return year + "-" + month + "-" + day;
}


/**
 * 前台ResultData对象
 * 
 * @param response,AJAX返回的对象
 * @param autoMsg,是否开启自动消息处理，true/false,默认为true
 * @param autoEx,是否开启自动异常处理，true/false,默认为true
 * @return
 */
function ResultData(response,autoMsg,autoEx){
	var res;
	if(response instanceof Object && 'success' in response){
		res = response;
	}else{
		res = eval("(" + response.responseText + ")");
	}
	
	this.success = res.success;
	this.message = res.message;
	this.exception = res.exception;
	this.userData = res.userData;
	this.result = res.data;
	
	if(autoEx == undefined || autoEx){// 判断是否开启自动异常处理，默认为开启
		if(this.success != true){
			if(this.exception != undefined && this.exception != ""){
				alert(this.exception);
				return;
			}
		}
	}
	
	if(autoMsg == undefined || autoMsg){// 判断是否开启自动消息处理，默认为开启
		if(this.message != undefined && this.message != ""){
			alert("来自后台的消息："+this.message);
		}
	}
}

/**
 * 判断后台处理是否成功
 */
ResultData.prototype.isSuccess = function(){
	if(this.success == undefined || this.success == ""){
		return false;
	}
	return this.success;
}

/**
 * 获取后台发送到前台的消息
 */
ResultData.prototype.getMessage = function(){
	if(this.message == undefined){
		return "";
	}
	return this.message;
}

/**
 * 获取后台发送到前台的异常
 */
ResultData.prototype.getException = function(){
	if(this.exception == undefined){
		return "";
	}
	return this.exception;
}

/**
 * 获取后台发送到前台的用户自定义数据
 */
ResultData.prototype.getUserData = function(key){
	if(this.userData == undefined || this.userData == "" || this.userData == "null"){
		return '';
	}
	return this.userData[key];
}

/**
 * 获取后台发送到前台的数据
 */
ResultData.prototype.getData = function(){
	return this.result;
}

function downLoadCurrentUserInfo(){
	var url = webRoot + "/hcm/user.t?m=getCurrentUser";
	
	var myCallback = function(oResponse) {
		var resultData = new ResultData(oResponse);
		var datas = resultData.getData();

		Assistant.User.id = datas.id;
		Assistant.User.name = datas.name;
		Assistant.User.homeId = datas.homeId;
		Assistant.User.homeName = datas.homeName;
		Assistant.User.roleId = datas.roleId;
		Assistant.User.roleName = datas.roleName;
	};
	jQuery.post(url, {}, myCallback, 'json');
}